package com.sky.controller.admin;


import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController // @Controller + @ResponseBody // 返回json数据
@Api(tags = "菜品相关接口") // swagger
@RequestMapping("/admin/dish") // 请求路径

public class DishController {


    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping()
    public Result addDish(@RequestBody DishDTO dto){
        log.info("新增菜品：{}",dto);
        dishService.addDish(dto);

        return Result.success();
    }

    /**
     * 分页查询菜品列表
     */
    @ApiOperation("分页查询菜品列表")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto){
        log.info("分页查询菜品列表：{}",dto);
        PageResult pageResult = dishService.page(dto);

        return Result.success(pageResult);
    }
    /**
     * 批量删除菜品
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        //@RequestParam 获取参数
        log.info("批量删除菜品：{}",ids);
        dishService.deleteBatch(ids);

        return Result.success();
    }

    /**
     * 根据id查询菜品信息（回显）
     */
    @ApiOperation("根据id查询菜品信息")
    @GetMapping("/{id}")
    public Result getById(@PathVariable long id){
        log.info("根据id查询菜品信息：{}",id);
        DishVO dishVO=dishService.getById(id);

        return Result.success(dishVO);

    }
    /**
     * 修改菜品信息
     */
    @ApiOperation("修改菜品信息")
    @PutMapping()
    public Result update(@RequestBody DishDTO dto ){

        log.info("修改菜品信息：{}",dto);
        dishService.update(dto);

        return Result.success();
    }
    /**
     * 菜品起售停售
     */

    @ApiOperation("菜品起售停售")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("菜品起售停售：{}",status,id);
        dishService.startOrStop(status,id);

        //如果是停售操作，则需要停售该菜品关联的套餐
        if(status == StatusConstant.DISABLE){
            log.info("停售菜品，需要将菜品关联的套餐也停售");
            List<Long> dishIds=new ArrayList<>(); //创建集合，用于存放当前菜品关联的套餐id
            dishIds.add(id); //添加当前菜品id

            //获取当前菜品关联的套餐id
            List<Long> setmealIds=setmealDishMapper.getSetmealIdsByDishIds(dishIds);

            if (setmealIds != null && setmealIds.size() > 0){
                for (Long setmealId : setmealIds){
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(status)
                            .build(); //创建Setmeal对象,设置修改的参数

                    setmealMapper.update(setmeal);//修改套餐状态
                }
            }

        }

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> selectByCategoryId(Long categoryid){
        log.info("根据分类id查询菜品");
        List<Dish> list = dishService.selectByCategoryId(categoryid);

        return Result.success(list);
    }

}












































